package com.atguigu.bigdata.chapter11.sql;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lzc
 * @Date 2022/9/9 14:09
 */
public class Flink04_SQL_Kafka_Upset {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        // 通过ddl方式建表, 直接和文件关联, 将来从这个表读取数据, 就自动从文件读取  (官方推荐使用这种)
        tEnv.executeSql("create table sensor(" +
                            " id string, " +
                            " ts int, " +
                            " vc int" +
                            ")with(" +
                            "  'connector' = 'kafka', " +
                            "  'topic' = 's1', " +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "  'properties.group.id' = 'atguigu', " +
                            "  'scan.startup.mode' = 'latest-offset', " +
                            "  'format' = 'csv'" +
                            ")");
        
        
        
        
        tEnv.executeSql("create table sensor_out(" +
                            " id string, " +
                            " vc int ," +
                            " primary key (`id`)NOT ENFORCED " + // 不检测主键的特性: 非空不重复
                            ")with(" +
                            "  'connector' = 'upsert-kafka', " +
                            "  'topic' = 's3', " +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "  'key.format' = 'json', " +
                            "  'value.format' = 'json'" +
                            ")");
    
        Table result = tEnv.sqlQuery("select id, sum(vc) vc from sensor group by id");
        result.executeInsert("sensor_out");
    
    
    }
}
/*
普通的kafka不能写入update change 数据, 只能写入 append only 数据

但是, 实时数仓中我们确实有需求要写入变化数据, 怎么办?
    使用upsert kafka
    
  为什么能够更新?

sensor_1, 1000, 10   插入(正常的kafka消息)   -> key: sensor_1
sensor_1, 1000, 30   插入(更新后的数据, 正常的kafka消息)-> key: sensor_1
    第二条数据是对第一条数据的更新, kakfa如何知道?
    
    靠什么指定key? 靠主键
    
    
 注意:
 
 如果数据有更新, 写入到kafka的时候, 必须使用 upset-kafka
 
 如果从 upset-kafka写入的topic中读取的时候, 可以使用upsert-kafka, 也可以使用普通的kafka 一般使用普通的kafka
    
 
 */